fix bugs with simplify filter (#1148)
authortsteven4 <13596209+tsteven4@users.noreply.github.com>
Fri, 28 Jul 2023 20:40:30 +0000 (14:40 -0600)
committerGitHub <noreply@github.com>
Fri, 28 Jul 2023 20:40:30 +0000 (14:40 -0600)
1. With the length option the last point deleted took the total error
over the specified limit.

2. When computing the total error if another point is deleted it was
possible to refer to an xte record that needed to be updated due to
the deletion of one of its neighbors.

reference/simplify_error_length.gpx
smplrout.cc

index 4a162239f5516945503d1a3763e74c1a48eb8df6..48a4357abd887328bb53cae6e7b70ecb9498e1d6 100644 (file)
         <time>2012-04-12T13:09:52Z</time>
         <speed>5.373000</speed>
       </trkpt>
+      <trkpt lat="48.190376647" lon="11.822674759">
+        <ele>541.600</ele>
+        <time>2012-04-12T13:09:59Z</time>
+        <speed>6.961000</speed>
+      </trkpt>
       <trkpt lat="48.190509081" lon="11.824321132">
         <ele>537.200</ele>
         <time>2012-04-12T13:10:15Z</time>
index 88d6db0e36da210d118ad2a18c08e3bdf742985c..3ddf224b757f62f58be660f822de14695d68b461 100644 (file)
@@ -235,13 +235,12 @@ void SimplifyRouteFilter::shuffle_xte(struct xte* xte_rec)
 
 void SimplifyRouteFilter::routesimple_tail(const route_head* rte)
 {
-  int i;
   if (!cur_rte) {
     return;
   }
 
   /* compute all distances */
-  for (i = 0; i < xte_count ; i++) {
+  for (int i = 0; i < xte_count ; i++) {
     compute_xte(xte_recs+i);
   }
 
@@ -257,7 +256,7 @@ void SimplifyRouteFilter::routesimple_tail(const route_head* rte)
   }
 
 
-  for (i = 0; i < xte_count; i++) {
+  for (int i = 0; i < xte_count; i++) {
     xte_recs[i].intermed->xte_rec = xte_recs+i;
   }
   // Ensure totalerror starts with the distance between first and second points
@@ -273,23 +272,8 @@ void SimplifyRouteFilter::routesimple_tail(const route_head* rte)
   while ((xte_count) &&
          (((limit_basis == limit_basis_t::count) && (count < xte_count)) ||
           ((limit_basis == limit_basis_t::error) && (totalerror < error)))) {
-    i = xte_count - 1;
+    int i = xte_count - 1;
     /* remove the record with the lowest XTE */
-    if (limit_basis == limit_basis_t::error) {
-      switch (metric) {
-      case metric_t::crosstrack:
-      case metric_t::relative:
-        if (i > 1) {
-          totalerror = xte_recs[i-1].distance;
-        } else {
-          totalerror = xte_recs[i].distance;
-        }
-        break;
-      case metric_t::length:
-        totalerror += xte_recs[i].distance;
-        break;
-      }
-    }
     (*waypt_del_fnp)(const_cast<route_head*>(rte),
                      const_cast<Waypoint*>(xte_recs[i].intermed->wpt));
     delete xte_recs[i].intermed->wpt;
@@ -306,8 +290,23 @@ void SimplifyRouteFilter::routesimple_tail(const route_head* rte)
     }
     xte_count--;
     free_xte(xte_recs+xte_count);
-    /* end of loop */
-  }
+
+    /* compute impact of deleting next point */
+    if (xte_count) {
+      if (limit_basis == limit_basis_t::error) {
+        i = xte_count - 1;
+        switch (metric) {
+        case metric_t::crosstrack:
+        case metric_t::relative:
+          totalerror = xte_recs[i].distance;
+          break;
+        case metric_t::length:
+          totalerror += xte_recs[i].distance;
+          break;
+        }
+      }
+    }
+  } /* end of loop */
   if (xte_count) {
     do {
       xte_count--;